package com.xiaoxie.boot.service.impl;

import com.xiaoxie.boot.bean.Menu;
import com.xiaoxie.boot.bean.MenuInfo;
import com.xiaoxie.boot.dao.MenuMapper;
import com.xiaoxie.boot.service.MenuService;
import com.xiaoxie.boot.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author xiaoxie
 * @create 2022-03-27 16:08
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;


    @Override
    public int insertSelective(Menu record) {
        int i = menuMapper.insertSelective(record);
        return i;
    }

    @Override
    public int updateByPrimaryKeySelective(Menu record) {
        int i = menuMapper.updateByPrimaryKeySelective(record);
        return i;
    }

    @Override
    public Map<String, Object> init(Integer id) {
        Map<String, Object> map = new HashMap<>(16);
        List<Menu> menuList = menuMapper.init(id);
        List<Menu> menuInfo = new ArrayList<>(16);
        for (Menu e : menuList) {
            Menu parent = new Menu();
            parent.setId(e.getId());
            parent.setPid(e.getPid());
            parent.setHref(e.getHref());
            parent.setTitle(e.getTitle());
            parent.setIcon(e.getIcon());
            parent.setTarget(e.getTarget());
            menuInfo.add(parent);
        }
        map.put("menuInfo", TreeUtil.toTree(menuInfo,0));
        return map;
    }

    @Override
    public List<Menu> queryAllMenu() {
        List<Menu> menus = menuMapper.queryAllMenu();
        return menus;
    }

    @Override
    public List<MenuInfo> initMenu() {
        List<MenuInfo> menuList = menuMapper.queryAllMenuInfo();
        List<MenuInfo> list = new ArrayList<>(16);
        for (MenuInfo e : menuList) {
            MenuInfo parent = new MenuInfo();
            parent.setId(e.getId());
            parent.setPid(e.getPid());
            parent.setHref(e.getHref());
            parent.setTitle(e.getTitle());
            parent.setIcon(e.getIcon());
            parent.setTarget(e.getTarget());
            list.add(parent);
        }
        return TreeUtil.toTree1(list,0);
    }

    @Override
    public int deleteMenuById(Integer id) {
        int i = menuMapper.deleteMenuById(id);
        return i;
    }

    @Override
    public List<MenuInfo> queryAllMenuInfo() {
        List<MenuInfo> menus = menuMapper.queryAllMenuInfo();
        return menus;
    }
}
